package example.tallydemo.MyView;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import example.tallydemo.R;

/**
 * Created by tuzha on 2018/4/16.
 */

public class SettingDialog extends DialogFragment
{
    View contentView ;
    String money;

    public interface OnDiaLogListener
    {
        void onDiaLogSetMoney(String money);
    }
    public OnDiaLogListener linstener; //监听器

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container
            , Bundle savedInstanceState)
    {
        // 创建View, 指定布局XML
        contentView  = inflater.inflate(R.layout.settingdialog, container);

        // 点击按钮时，关闭对话框
        Button button = contentView.findViewById(R.id.id_dialog_ok);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dismiss(); // 关闭对话框
                readUserSet();
            }
        });

        return contentView;
    }

    public void readUserSet()
    {
        money = ((EditText) contentView.findViewById(R.id.id_dialog_editText)).getText().toString();

        if (linstener != null)
        {
            linstener.onDiaLogSetMoney(money);
        }
    }
    @Override
    public void onStart()
    {
        super.onStart();

        // 当对话框显示时，调整对话框的窗口位置
        Window window = getDialog().getWindow();
        if (window != null)
        {
            window.setBackgroundDrawable( new ColorDrawable(Color.WHITE));

            // 设置对话框的窗口显示
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.dimAmount=0.3f; // 背景灰度
            //lp.gravity = Gravity.BOTTOM; // 靠下显示
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }
    }

    public String getMoney()
    {
        return this.money;
    }

}
